home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: When to use "->" vs "." when calling Member functions
- Date: Wed, 17 Jan 1996 02:08:39 GMT
- Organization: Netcom
- Message-ID: <30fc54cc.83447808@nntp.ix.netcom.com>
- References: <4dhea1$6v8@ornews.intel.com>
- NNTP-Posting-Host: ix-dc7-23.ix.netcom.com
- X-NETCOM-Date: Tue Jan 16 6:08:20 PM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- thurman_b_miller@ccm2.hf.intel.com (Thurman Miller) wrote:
-
- |>I'm confused, so please no harsh remarks :)
- |>
- |>If I've got:
- |>
- |>class Cfoo
- |>{
- |> something * getptr();
- |> somethingelse* m_other;
- |>}
- |>
- |>something * foo::getptr()
- |>{
- |> return m_other;
- |>}
- |>
- |>
- |>Now...if I'm in another class....
- |>
- |> Cfoo foo;
- |> somethingelse* = foo.getptr();
- |>
- |>why doesn't the following work?
- |>
- |> somethingelse* = foo->getptr();
- |>
- |>I get compile error about no "->" overloaded operator....
- |>
- |>Can someone point out the obvious when I use one notation over
- |>another?
-
- It's really quite simple. Let's ignore overloading operator->. If
- foo is a pointer to Cfoo, then you need to use foo->getptr(). If foo
- is an instance of Cfoo or a reference to Cfoo, then you must use
- foo.getptr().
-
- Example:
-
- class A {
- public:
- void f();
- };
-
- A a; // a is an instance of A
- A* pa = new A; // pa is a pointer to A
- A& ra = a; // ra is a reference to A
-
- a.f();
- (&a)->f(); // if a is an instance of A, then &a is a
- // pointer to A
- pa->f();
- (*pa).f(); // if pa is a pointer to A, *pa is an instance
-
- // of A. By definition, the previous
- // statement and this are equivalent
- ra.f();
- (&ra)->f(); // if ra is a reference to A, then &ra is a
- // pointer to A
-
-
- Michael M Rubenstein
-